import axios from "axios";
const baseUrl = "http://ludir.cn:8888/api/private/v1/";
//导入进度条插件
import NProgress from "nprogress";
//导入进度条样式
import "nprogress/nprogress.css";
// 默认配置 基础路径直接配置到axios请求头里
axios.defaults.baseURL = baseUrl;
// 添加请求拦截器
axios.interceptors.request.use(
  function(config) {
    // 在发送请求之前做些什么
    let getToken = localStorage.getItem("tokenLogin") || "";
    config.headers["Authorization"] = getToken;
    NProgress.start();
    return config;
  },
  function(error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);
// 添加响应拦截器
axios.interceptors.response.use(
  function(response) {
    NProgress.done();
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    return response.data;
  },
  function(error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);
// 封装api
// 登录接口
export let checkLogin = params => axios.post("login", params);
// 获取用户列表
export let getUsers = params => axios.get("users", params);
// 获取单个用户数据
export let getSingleUser = params => axios.get(`users/${params}`);
// 修改用户状态
export let changeUserState = params =>
  axios.put(`users/${params.uid}/state/${params.type}`);
// 编辑用户数据
export let editUser = params => axios.put(`users/${params.id}`, params);
// 删除用户
export let delUser = params => axios.delete(`users/${params}`);
// 添加用户
export let addUser = params => axios.post(`users`, params);
// 分配角色
export let assignRoles = params => axios.put(`users/${params.id}/role`, params);
// 获取角色列表
export let getRoleList = () => axios.get(`roles`);
// 获取权限列表
export let getRightList = params => axios.get(`rights/${params.type}`);
// 删除角色权限
export let delRoleRight = params =>
  axios.delete(`roles/${params.roleId}/rights/${params.rightId}`);
// 查询角色
export let getSingleRole = params => axios.get(`roles/${params}`);
// 编辑角色
export let editRole = params => axios.put(`roles/${params.roleId}`, params);
// 添加角色
export let addRole = params => axios.post(`roles`, params);
// 删除角色
export let delRole = params => axios.delete(`roles/${params}`);
// 角色授权
export let authRole = (roleId, rids) =>
  axios.post(`roles/${roleId}/rights`, rids);
// 动态获取权限菜单
export let getMenus = () => axios.get("menus");
// 获取商品列表
export let getGoodsList = params => axios.get(`goods`, params);
// 查询单个商品
export let getSingleGood = params => axios.get(`goods/${params}`);
// 删除商品
export let delGood = params => axios.delete(`goods/${params}`);
// 编辑商品
export let editGoods = params => axios.put(`goods/${params.id}`, params);
// 添加商品
export let addGoods = params => axios.post(`goods`, params);
// 获取商品分类列表
export let getCategoriesList = params => axios.get(`categories`, params);
// 添加商品分类
export let addCat = params => axios.post(`categories`, params);
// 删除商品分类
export let delCat = params => axios.delete(`categories/${params}`);
// 查询商品分类
export let getCat = id => axios.get(`categories/${id}`);
// 编辑商品分类
export let editCat = (id, params) => axios.put(`categories/${id}`, params);
// 获取商品分类参数列表
export let getCatAttr = params =>
  axios.get(`categories/${params.id}/attributes`, { params: params });
// 添加商品分类参数
export let addCatAttr = (id, params) =>
  axios.post(`categories/${id}/attributes`, params);
// 删除商品分类参数
export let delCatAttr = (id, attrid) =>
  axios.delete(`categories/${id}/attributes/${attrid}`);
// 编辑商品分类参数
export let editCatAttr = (id, attrid, params) =>
  axios.put(`categories/${id}/attributes/${attrid}`, params);
// 获取订单列表
export let getOrderList = params => axios.get(`orders`, { params: params });
// 修改订单信息
export let editOrder = params => axios.put(`orders/${params.id}`, params);
// 查询订单详情
export let queryOrder = params => axios.get(`orders/${params}`);
// echarts 数据获取
export let getReports = params => axios.get("reports/type/1", params);
